﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Security.Principal;
using Microsoft.Practices.Unity;
using zic_dotnet;
using stackFluent.EF;

namespace stackFluent.Web {
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new UserInject());
        }

        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

        public override void Init() {
            //this.PostAuthenticateRequest += SecurityExtensions.PostAuthenticateRequestHandler;
            base.Init();
        }

        protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            Container = new UnityContainer();
            this.RegisterTypes();
        }

        public static IUnityContainer Container { get; private set; }

        protected virtual void RegisterTypes() {
            //Container
            //.RegisterType<DatabaseFactory, DatabaseFactory>(new HttpContextLifetimeManager<EFFactory>())
            //.RegisterType<UnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<UnitOfWork>())

            //.RegisterType<CategoryRepository, CategoryRepository>(new HttpContextLifetimeManager<CategoryRepository>())
            //.RegisterType<UserRepository, UserRepository>(new HttpContextLifetimeManager<UserRepository>())
            //.RegisterType<SiteRepository, SiteRepository>(new HttpContextLifetimeManager<SiteRepository>())
            //.RegisterType<TechRepository, TechRepository>(new HttpContextLifetimeManager<TechRepository>())
            //    //.RegisterType<RelaSiteSiteRepository, RelaSiteSiteRepository>(new HttpContextLifetimeManager<RelaSiteSiteRepository>())
            //.RegisterType<RelaTechSiteRepository, RelaTechSiteRepository>(new HttpContextLifetimeManager<RelaTechSiteRepository>())
            //.RegisterType<RelaUserSiteRepository, RelaUserSiteRepository>(new HttpContextLifetimeManager<RelaUserSiteRepository>())
            //.RegisterType<RelaUserTechRepository, RelaUserTechRepository>(new HttpContextLifetimeManager<RelaUserTechRepository>())
            //.RegisterType<RelaUserUserRepository, RelaUserUserRepository>(new HttpContextLifetimeManager<RelaUserUserRepository>())
            //    //.RegisterType<RelaTechTechRepository, RelaTechTechRepository>(new HttpContextLifetimeManager<RelaTechTechRepository>())

            //.RegisterType<AnswerRepository, AnswerRepository>(new HttpContextLifetimeManager<AnswerRepository>())
            //.RegisterType<QuestionRepository, QuestionRepository>(new HttpContextLifetimeManager<QuestionRepository>())
            //.RegisterType<ResourceRepository, ResourceRepository>(new HttpContextLifetimeManager<ResourceRepository>())
            //.RegisterType<ChatRepository, ChatRepository>(new HttpContextLifetimeManager<ChatRepository>())
            //.RegisterType<ReplyRepository, ReplyRepository>(new HttpContextLifetimeManager<ReplyRepository>())

            //.RegisterType<TagRepository, TagRepository>(new HttpContextLifetimeManager<TagRepository>())
            //.RegisterType<TagNodeRepository, TagNodeRepository>(new HttpContextLifetimeManager<TagNodeRepository>())
            //.RegisterType<TagPostRepository, TagPostRepository>(new HttpContextLifetimeManager<TagPostRepository>())
            //.RegisterType<RelaTagTagRepository, RelaTagTagRepository>(new HttpContextLifetimeManager<RelaTagTagRepository>())

            //.RegisterType<VoteAnswerRepository, VoteAnswerRepository>(new HttpContextLifetimeManager<VoteAnswerRepository>());

            DependencyResolver.SetResolver(new MyDependencyResolver(Container));
        }
    }
}